package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.InfoOrTypeListDTO;
import com.huaxin.common.Entity.InfoType;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/28.
 * 首页新闻列表
 */

public class NewsListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    RecyclerView listView;

    @BindView(R.id.more)
    TextView           more;
    @BindView(R.id.toolbar)
    Toolbar            toolbar;
    @BindView(R.id.recyclerview)
    RecyclerView       mRecyclerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    private int mPage = 0;
    private InfoType _infoType;
    private List<InfoOrTypeListDTO.InfosBean> mFlagNewsBeans = new ArrayList<>();
    private HxNewsListAdapter mListAdapter;
    private final static int TYPE_REFRESH = 1;
    private final static int TYPE_MORE    = 2;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_info_list;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    public static void start(Context context, InfoType infoType) {
        Intent intent = new Intent(context, NewsListActivity.class);
        intent.putExtra(Constants.IK_INFO_TYPE, infoType);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        _infoType = extras.getParcelable(Constants.IK_INFO_TYPE);
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(toolbar);
        toolbar.setTitle(_infoType.getTitle());

        mListAdapter = new HxNewsListAdapter(this, mFlagNewsBeans);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.setAdapter(mListAdapter);
        mSwipeLayout.setOnRefreshListener(this);
        mListAdapter.setOnLoadMoreListener(this, mRecyclerview);
        mListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InfoOrTypeListDTO.InfosBean bean = mListAdapter.getData().get(position);
                CommonWebActivity.start(NewsListActivity.this, bean);
            }
        });
    }

    @Override
    public void requestData() {
        updateLoadStatus(LOADING);

        ApiRequestData(TYPE_REFRESH, 0);
    }


    private void ApiRequestData(final int refreshState, int page) {
        Call<InfoOrTypeListDTO> call = getRequestApi().getInfoOrType(
                _infoType.getId(), page);
        autoClose(call);
        call.enqueue(new Callback<InfoOrTypeListDTO>() {
            @Override
            public void onResponse(Call<InfoOrTypeListDTO> call, Response<InfoOrTypeListDTO>
                    response) {
                InfoOrTypeListDTO infoOrTypeListDTO = response.body();
                if (response.isSuccessful() && infoOrTypeListDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<InfoOrTypeListDTO.InfosBean> beanList = infoOrTypeListDTO.getInfos();
                    switch (refreshState) {
                        case TYPE_REFRESH://下拉刷新
                            if (beanList.size() == 0) {
                                mListAdapter.getData().clear();

                            } else {
                                mListAdapter.setNewData(beanList);

                            }
                            mSwipeLayout.setRefreshing(false);
                            mListAdapter.setEnableLoadMore(true);
                            mListAdapter.loadMoreComplete();
                            break;
                        case TYPE_MORE://上拉加载更多
                            if (beanList.size() == 0) {
                                mListAdapter.loadMoreEnd(false);
                            } else {
                                mListAdapter.setEnableLoadMore(true);
                                mListAdapter.addData(beanList);
                                mListAdapter.loadMoreComplete();
                            }
                            mSwipeLayout.setEnabled(true);

                            break;
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                    // ToastUtil.showToast("数据异常");
                }
            }

            @Override
            public void onFailure(Call<InfoOrTypeListDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        mPage = 0;
        ApiRequestData(TYPE_REFRESH, mPage);
    }

    @Override
    public void onLoadMoreRequested() {
        //少于一页的数量
        if (mListAdapter.getData().size() < 10) {
            mListAdapter.loadMoreEnd(true);
        } else {
            mPage++;
            //加载更多操作,下拉暂时不可用
            mSwipeLayout.setEnabled(false);
            ApiRequestData(TYPE_MORE, mPage);
            Log.d(TAG, "onRefresh: ---加载更多->");
        }
    }
}
